package sitesettinghandler

import (
	"github.com/gin-gonic/gin"
	"log"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/coupon"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

//优惠券列表
func CouponList(c *gin.Context) {
	param := &struct {
		BaseListReq request.BaseListReq
	}{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	//获取数据
	data := dao.NewSiteSettingDao().CouponListDao(&param.BaseListReq)
	//错误处理
	if data.Err != nil {
		Response(c)("", CodeInternalServerError, data.Data)(ServerError)
	}

	Response(c)("", CodeSuccess, ResponseList{
		PageNo:     param.BaseListReq.PageNo,
		PageSize:   param.BaseListReq.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  0,
		Data:       data.Unwrap(),
	})(OK)
}

//优惠券编辑
func CouponEdit(c *gin.Context) {
	param := &coupon.Coupon{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	Response(c)("修改成功", CodeUpdateSuccess, dao.NewSiteSettingDao().CouponEditDao(param.Id, param).Unwrap())(OK)
}

//优惠券新增
func CouponSave(c *gin.Context) {
	param := &coupon.Coupon{}
	dingo.ErrorIf(c.ShouldBindJSON(param)).Unwrap()

	log.Println(param)

	//if param.EndTime == 0 || param.StartTime == 0 || param.Threshold == 0 || param.Name == "" || param.Type == 0 || param.Status == 0 {
	//	Response(c)("新增失败", CodeInvalidParam, false)(OK)
	//}

	Response(c)("新增成功", CodeUpdateSuccess, dao.NewSiteSettingDao().CouponSaveDao(param).Unwrap())(OK)
}

//优惠券删除
func CouponDelete(c *gin.Context) {
	param := &struct {
		Id int `json:"id"`
	}{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	Response(c)("删除成功", CodeUpdateSuccess, dao.NewSiteSettingDao().CouponDeleteDao(param.Id).Unwrap())(OK)
}
